﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SD.Database;

namespace smallDeimos.Game.Alliance
{
    public partial class JoinAlliance : System.Web.UI.Page
    {
        int UserId;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
            if (UserFunctions.GetPlayerAlliance(UserId) >= 0)
            {
                Label1.Text = "You're in alliance so you cannot create new one";
                Button1.Enabled = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int id = AllianceFunctions.GetAllianceId(TextBox1.Text);
            if (id == -1)
            {
                Label1.Text = "Wrong alliance name";
                return;
            }
            if (AllianceFunctions.GetAlliancePassword(id) != TextBox2.Text)
            {
                Label1.Text = "Wrong password";
                return;
            }
            else
            {
                SD.Database.AllianceFunctions.AddPlayerToAlliance(UserId, id);
                Label1.Text = "You have joined to alliance " + TextBox1.Text;
            }
        }
    }
}
